package org.jcms.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jcms.model.Book;
import org.jcms.service.BookServiceI;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class StorehouseAddBookCount extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public StorehouseAddBookCount() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 ApplicationContext ac = WebApplicationContextUtils
   				.getRequiredWebApplicationContext(this.getServletContext());
   		BookServiceI bookServiceI = (BookServiceI)ac.getBean("bookServiceImpl");
   		
		String retJson = "{\"error\":@err,\"msg\":\"@msg\"}";
		String err = "1";
		String msg = "";
		String bookIsbn = request.getParameter("bookIsbn");
		String addNumber = request.getParameter("addNumber");
		try {
              if(bookIsbn==null||addNumber==null||"".equals(bookIsbn)||"".equals(addNumber)){
            	  msg="教材Isbn和入库数量不能为空";
              }else{
            	  int intAddNumber = Integer.parseInt(addNumber);
            	  if(intAddNumber<=0){
            		  msg="教材入库量不能小于等于0";
            	  }else{
            		  Book book = bookServiceI.findById(bookIsbn);
                 	 if(book==null){
                 		 msg="不能入库非指的书藉"; 
                 	 }else{
                 		 book.setBookStock(book.getBookStock()+intAddNumber);
                 		 bookServiceI.update(book);
                 		 err="0";
                 		 msg="教材成功入库";
                 	 }
            	  }
              }
		} catch (Exception e) {

		} finally {
			retJson = retJson.replace("@err", err);
			retJson = retJson.replace("@msg", msg);
			response.getOutputStream().write(retJson.getBytes("utf-8"));
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
